<?php
require_once('../../../../conf/config.php.inc');
require_once(SYBMAS_LIB_PATH.'/liblogin.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/layout.php.inc');
require_once(SYBMAS_LIB_PATH.'/libsybmas.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/menu.php.inc');
require_once('../../lib/libtablas.php.inc');
require_once('../privilegios/otorgarRevocar.php.inc');
verificar_inicio_sesion();
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/sybmasstyle.css">
<script src="<?php echo LAYOUT_URI?>/jquery-1.2.1.min.js"
	type="text/javascript"></script>
<script src="<?php echo LAYOUT_URI?>/menu-collapsed.js"
	type="text/javascript"></script>
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/style.css" >
<link rel="stylesheet" type="text/css"
	href="<?php echo JAVASCRIPT_URI?>/tabs/tabs.css">
<script src="<?php echo JAVASCRIPT_URI?>/tabs/tabs.js"
	type="text/javascript"></script>
<script type="text/javascript" src="<?php echo JAVASCRIPT_URI?>/commons.js"></script>
<script type="text/javascript" src="tabla.js"></script>
<title>.:: Sybmas - Detalle de tabla</title>
<!--[if lt IE 8]>
   <style type="text/css">
   li a {display:inline-block;}
   li a {display:block;}
   </style>
   <![endif]-->
</head>
<body>
<script src="<?php echo JAVASCRIPT_URI?>/tooltip/wz_tooltip.js"
	type="text/javascript"></script>
<center>
<div id="container">
<div id="header"><?php generar_header();?></div>
<div id="scene">
<div id="modulos">
<ul id="menu" style="width: 150px;">
<?php generar_menu();?>
</ul>
</div>

<div id="title">Detalle de tabla</div>
<div id="main">
<?php 
$dbname=false;
if(array_key_exists('dbname',$_GET)){
	$dbname=$_GET['dbname'];
}
$id=$_GET['id'];
$tablename='';
?> 
<?php
$cambio_bd_exitoso=true;
$sybuser_connection=start_default_sybuser_connection();

if($dbname){
	$cambio_bd_exitoso=sybase_select_db($dbname,$sybuser_connection);
}else{
	$dbname=$_SESSION['sybmas.user.dbname'];
}

if($cambio_bd_exitoso){
	sybase_min_server_severity(11);
	$query="SELECT o.id,o.name, o.crdate, u.name AS owner ,s.name AS segment ".
		"FROM sysobjects o, sysusers u, syssegments s, sysindexes i ".
		"WHERE o.id=$id ".
		"AND i.indid<2 ".
		"AND o.uid=u.uid ".
		"AND i.id=o.id ".
		"AND i.segment=s.segment";

	$result=sybase_query($query,$sybuser_connection);
	$data=sybase_fetch_array($result);
	$tablename=$data['name'];
	$tableowner=$data['owner'];
	?>
<div id="tab_panel">
<div id="tab_header">
<ul>
	<li id="selected"><a href="#" id="ctl_tabgeneral" onclick="showTab(this.id,0)"><?php echo $tablename?></a></li>
	<li><a href="#" id="ctl_tabcolumnas" onclick="showTab(this.id,1)">Columnas</a></li>
	<li><a href="#" id="ctl_tabrestricciones" onclick="showTab(this.id,2)">Restricciones</a></li>
	<li><a href="#" id="ctl_tabindices" onclick="showTab(this.id,3)">Indices</a></li>
	<li><a href="#" id="ctl_tabparticiones" onclick="showTab(this.id,4)">Particiones</a></li>
	<li><a href="#" id="ctl_tabpermisos" onclick="showTab(this.id,5)">Permisos</a></li>	
</ul>
</div>
<div id="tab_content">

<div class="tab_scene" style="display: block">

<table width="80%" border="1">
	<caption>Informacion general de la tabla</caption>
	<tr>
		<th>id</th>
		<td><?php echo $data['id']?></td>
	</tr>
	<tr>
		<th>base de datos</th>
		<td><?php echo !empty($dbname)?$dbname:$_SESSION['sybmas.user.dbname']?></td>
	</tr>
	<tr>
		<th>nombre</th>
		<td><?php echo $data['name']?></td>
	</tr>
	<tr>
		<th>propietario</th>
		<td><?php echo $data['owner']?></td>
	</tr>
	<tr>
		<th>fecha creacion</th>
		<td><?php echo $data['crdate']?></td>
	</tr>
	<tr>
		<th>segmento</th>
		<td><?php echo $data['segment'];?></td>
	</tr>
</table>
</div>
<!-- tab_scene --> 
<div class="tab_scene">
<?php
sybase_free_result($result);
$query="SELECT c.colid,c.name,t.name AS type,c.length,c.prec,c.scale, ".
		"CASE convert(bit, (c.status & 8)) ". 
		"WHEN 0 THEN 'No' ".
		"ELSE 'Si' ".
		"END AS nullable, ".
		"CASE convert(bit, (c.status & 0x80)) ".
		"WHEN 0 THEN 'No' ".
		"ELSE 'Si' ".
		"END AS ident ".		
		"FROM syscolumns c, systypes t ".
		"WHERE c.id=$id ".
		"AND c.usertype*=t.usertype";

$result=sybase_query($query,$sybuser_connection);

?>
<div class="controls">
<form method="POST" action="../columna/agregar.php" target="constraints_dialog" onsubmit="postDialog('constraints_dialog')">
<input type="hidden" name="id" value="<?php echo $id?>">
<input type="hidden" name="tableowner" value="<?php echo $tableowner?>">
<input type="hidden" name="tablename" value="<?php echo $tablename?>">
	<?php if($dbname){?>
	<input type="hidden" name="dbname" value="<?php echo $dbname?>" >
	<?php }?>
	<input type="submit" value="Agregar columna">
</form>
</div>
<hr>
<table width="80%" border="1">
	<caption>Columnas</caption>
	<tbody>
		<tr>
			<th>colid</th>
			<th>nombre</th>
			<th>tipo</th>
			<th>longitud</th>
			<th>precision</th>
			<th>escala</th>
			<th>nulos</th>
			<th>identidad</th>
		</tr>
		<?php
		while($data=sybase_fetch_array($result)){
			$column_name= $data['name'];
			?>
		<tr>
			<td><?php echo $data['colid']?></td>
			<td><?php echo $data['name']?></td>
			<td><?php echo $data['type']?></td>
			<td><?php echo $data['length']?></td>
			<td><?php echo $data['prec']?></td>
			<td><?php echo $data['scale']?></td>
			<td><?php echo $data['nullable']?></td>
			<td><?php echo $data['ident']?></td>
			<td>
			<?php 
				$param=	"".
						"cname=$column_name".
						"&tablename=$tablename". 				
						"&tableowner=$tableowner".
						($dbname?"&dbname=$dbname":"");
			?>			
			<a
			href="#"
			onclick="showConfirmDialog('../columna/borrar.php','<?php echo $param;?>','¿Desea continuar con la eliminacion de la columna \'<?php echo $column_name?>\'?')"> 
			<img src="<?php echo IMG_URI?>/document_delete.png"
			alt="Eliminar columna" title= "Eliminar columna" width="15" height="15" border="0">
			</a>		
		</td>
		</tr>
		<?php
		}		
		?>
	</tbody>
</table>
</div>
<!-- tab_scene --> 

<div class="tab_scene">
<?php
@sybase_free_result($result);
$query="sp_helpconstraint $tablename";

sybase_set_message_handler("void_msg_handler",$sybuser_connection);
$result=@sybase_query($query,$sybuser_connection);
sybase_set_message_handler("msg_handler",$sybuser_connection);

$query="SELECT count(*) FROM sysindexes ".
		"WHERE id=$id AND status & 2048 = 2048";
$result_haspk=sybase_query($query,$sybuser_connection);
$data=sybase_fetch_row($result_haspk);
$haspk=$data[0];
sybase_free_result($result_haspk);

?>
<div class="controls">
<form method="GET" action="../restriccion/dispatcher.php" target="constraints_dialog" onsubmit="postDialog('constraints_dialog')">
<input type="hidden" name="id" value="<?php echo $id?>">
<input type="hidden" name="tableowner" value="<?php echo $tableowner?>">
<input type="hidden" name="tablename" value="<?php echo $tablename?>">
	<?php if($dbname){?>
	<input type="hidden" name="dbname" value="<?php echo $dbname?>" >
	<?php }?>
	<input type="submit" value="Agregar restriccion">
	<input type="radio" name="constraintType" <?php echo $haspk?"disabled='disabled'":"";?> value="pk">Llave primaria
	<input type="radio" name="constraintType" value="u">De unicidad
	<input type="radio" name="constraintType" value="fk" checked="checked">Llave foranea
	<input type="radio" name="constraintType" value="c">Check	
</form>
</div>
<hr>
<table width="80%" border="1">
	<caption>Restricciones</caption>
	<tr>
		<th>nombre</th>
		<th>definicion</th>
	</tr>
	<?php
	while($data=@sybase_fetch_array($result)){
		if(!array_key_exists('name',$data)){
			continue;
		}
		$cname=trim($data['name']);
		
		?>
	<tr>
		<td><?php echo $cname?></td>
		<td><?php echo $data['definition']?></td>
		<td>
			<?php 
				$param=	"".
						"cname=$cname".
						"&tablename=$tablename". 				
						"&tableowner=$tableowner".
						($dbname?"&dbname=$dbname":"");
			?>			
			<a
			href="#"
			onclick="showConfirmDialog('../restriccion/borrar.php','<?php echo $param;?>','¿Desea continuar con la eliminacion de la restriccion \'<?php echo $cname?>\'?')"> 
			<img src="<?php echo IMG_URI?>/document_delete.png"
			alt="Eliminar restriccion" title="Eliminar restriccion" width="15" height="15" border="0">
			</a>		
		</td>
	</tr>
	<?php
	}
	?>
</table>
</div>
<!-- tab_scene --> 
<div class="tab_scene">
<?php
@sybase_free_result($result);
$query="sp_helpindex $tablename";

sybase_set_message_handler("void_msg_handler",$sybuser_connection);
$result=@sybase_query($query,$sybuser_connection);
sybase_set_message_handler("msg_handler",$sybuser_connection);
?>
<div class="controls">
<form method="POST" action="../../indices/crear/indice.php">
<input type="hidden" name="id" value="<?php echo $id?>">
<input type="hidden" name="tableowner" value="<?php echo $tableowner?>">
<input type="hidden" name="tablename" value="<?php echo $tablename?>">
	<?php if($dbname){?>
	<input type="hidden" name="dbname" value="<?php echo $dbname?>" >
	<?php }?>
	<input type="submit" value="Crear &iacute;ndice">
</form>
</div>
<hr>
<table width="80%" border="1">
	<caption>Indices</caption>
	<tr>
		<th>nombre</th>
		<th>descripcion</th>
		<th>llaves</th>
		<th>filas max/pag.</th>
		<th>fact llenado</th>
		<th>pag. reserv</th>
	</tr>
	<?php
	while($data=@sybase_fetch_array($result)){
		if(!array_key_exists('index_name',$data)){
			continue;
		}
		$indexname=trim($data['index_name']);
		?>
	<tr>
		<td><?php echo $indexname?></td>
		<td><?php echo $data['index_description']?></td>
		<td><?php echo $data['index_keys']?></td>
		<td><?php echo $data['index_max_rows_per_page']?></td>
		<td><?php echo $data['index_fillfactor']?></td>
		<td><?php echo $data['index_reservepagegap']?></td>
		<td>
			<a
			href="../../indices/borrar/indice.php?id=
			<?php 
			echo $id;
			echo "&indexname=".$indexname;
			echo "&tablename=".$tablename;
			if($dbname){
				echo "&dbname=$dbname";	
			}
			?>"> <img src="<?php echo IMG_URI?>/document_delete.png"
			alt="Eliminar indice" title="Eliminar indice" width="15" height="15" border="0">
			</a>
		
		</td>
	</tr>
	<?php
	}
	?>
</table>
</div>
<!-- tab_scene --> 

<div class="tab_scene">

<?php //particiones
	@sybase_free_result($result);
	$query="select	partitionid, firstpage, controlpage, ".
		"ptn_data_pages = ptn_data_pgs($id, partitionid) ".
		"from syspartitions ". 
		"where id = $id";

	$result=@sybase_query($query,$sybuser_connection);
	if($result){
	
	$imprime_particiones=sybase_num_rows($result);
	?>
<table width="80%" border="1">
	<caption>Particiones</caption>
	<tr>
		<th>id particion</th>
		<th>primera pag.</th>
		<th>pag. control</th>
		<th>ptn pag datos</th>
	</tr>
	<?php
	while($data=@sybase_fetch_array($result)){
		?>
	<tr>
		<td><?php echo $data['partitionid']?></td>
		<td><?php echo $data['firstpage']?></td>
		<td><?php echo $data['controlpage']?></td>
		<td><?php echo $data['ptn_data_pages']?></td>
	</tr>
	<?php
	}
	?>
</table>
	<?php
	@sybase_free_result($result);
	if($imprime_particiones){
		$query="select count(*) 'Partitions', ".
	"avg(ptn_data_pgs($id, partitionid)) 'Average_Pages', ". 
	"max(ptn_data_pgs($id, partitionid)) 'Maximum_Pages', ".
	"min(ptn_data_pgs($id, partitionid)) 'Minimum_Pages', ".
	"convert(float,max(ptn_data_pgs($id, partitionid)))/avg(ptn_data_pgs($id, partitionid)) 'Ratio' ".
	"from syspartitions ". 
	"where  id = $id";
		$result=@sybase_query($query,$sybuser_connection);
		?>
<table width="80%" border="1">
	<tr>
		<th>Particiones</th>
		<th>Pags. promedio</th>
		<th>Pags. max</th>
		<th>Pags. min</th>
		<th>Ratio (prom/max)</th>
	</tr>
	<?php
	while($data=@sybase_fetch_array($result)){
		?>
	<tr>
		<td><?php echo $data['Partitions']?></td>
		<td><?php echo $data['Average_Pages']?></td>
		<td><?php echo $data['Maximum_Pages']?></td>
		<td><?php echo $data['Minimum_Pages']?></td>
		<td><?php echo $data['Ratio']?></td>
	</tr>
	<?php
	}
	?>
</table>
	<?php
	sybase_free_result($result);
	
	}//if($imprime_particiones)
	
	}//if($result)
	?></div>
<!-- tab_scene -->

<div class="tab_scene">

<?php
$usuario_db=obtener_usuario_db();
sybase_close($sybuser_connection);
$sybuser_connection=start_default_sybuser_connection();
sybase_select_db($dbname,$sybuser_connection);
$query="sp_helprotect \"$tableowner.$tablename\",\"$usuario_db\"";
//echo $query."<br>";
$result=sybase_query($query,$sybuser_connection);

?>
<div class="controls">
<?php 
//echo 'tableowner: '." $tableowner ".obtener_nombre_usuario()."<br>";
//echo 'usuario_en_rol(\'sa_role\'): '.usuario_en_rol('sa_role').'<br>';
//echo 'usuario_es_dbo(): '.usuario_es_dbo($dbname)."<br>";
$userHasGrantOption=getPrivilegesWithGrantOption($dbname,$tableowner,$tablename,obtener_usuario_db());
//echo "userHasGrantOption $userHasGrantOption<br>";
if($tableowner==obtener_usuario_db() || usuario_en_rol('sa_role') || $userHasGrantOption ){?>
<form method="POST" action="../privilegios/otorgarRevocar.php" 
	target="otorgarRevocar_dialog" onsubmit="postDialog('otorgarRevocar_dialog')">
<input type="hidden" name="id" value="<?php echo $id?>">
<input type="hidden" name="tableowner" value="<?php echo $tableowner?>">
<input type="hidden" name="tablename" value="<?php echo $tablename?>">
	<?php if($dbname){?>
	<input type="hidden" name="dbname" value="<?php echo $dbname?>" >
	<?php }?>
	<?php if($userHasGrantOption){?>
	<input type="hidden" name="userHasGrantOption" value="<?php echo $userHasGrantOption?>" >
	<?php }?>
	<input type="submit" value="Otorgar/revocar privilegios">
</form>
<?php }?>
</div>
<hr>


<table width="80%" border="1">
	<caption>Permisos otorgados sobre la tabla para el usuario: <b><?php echo obtener_nombre_usuario()?></b></caption>
	<tr>
		<th>Otorga</th>
		<th>Recibe</th>
		<th>Tipo</th>
		<th>Accion</th>
		<th>Objeto</th>
		<th>Columna</th>
		<th>Puede otorgar permisos</th>
	</tr>
	<?php
	while($data=@sybase_fetch_array($result)){
		if(!array_key_exists('grantor',$data)){
			continue;
		}
		?>
	<tr>
		<td><?php echo $data['grantor']?></td>
		<td><?php echo $data['grantee']?></td>
		<td><?php echo $data['type']?></td>
		<td><?php echo $data['action']?></td>
		<td><?php echo $data['object']?></td>
		<td><?php echo $data['column']?></td>
		<td><?php echo $data['grantable']?></td>
	</tr>
	<?php
	}
	?>
</table>
</div>
<!-- tab_scene --> 


</div>
<!-- tab_content --> <?php	
}//if($cambio_bd_exitoso)
?>

 <?php
//Cerrando conexiones a la base de datos
//	sybase_close($sybuser_connection);
?></div>
</div>
<!-- main --></div>
<!-- scene --> <?php generar_footer();?></div>
</center>
</body>
</html>
